package alarm;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.table.DefaultTableModel;

import view.FrameWork;

public class RemoveAlarmPanel extends JPanel {
	private static final long serialVersionUID = 2089826867791118645L;

	private JScrollPane scrTblAlarms = null;
	private JTable tblAlarms = null;
	private JTextArea lblGeneralText;
	private JButton bttClose = null;
//	private JButton bttRemoveAll = null;
	private JButton bttRemoveSelected = null;

	/**
	 * This is the default constructor
	 */
	public RemoveAlarmPanel() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setPreferredSize(new java.awt.Dimension(493, 269));
		this.setLayout(null);
		this.add(getLblGeneralText());
		this.add(getScrTblAlarms());
		this.add(getBttRemoveSelected());
//		this.add(getBttRemoveAll());
		this.add(getBttClose());
	}

	/**
	 * This method initializes scrTblAlarms	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getScrTblAlarms() {
		if (scrTblAlarms == null) {
			scrTblAlarms = new JScrollPane();
			scrTblAlarms.setBounds(12, 55, 476, 168);
			scrTblAlarms.setViewportView(getTblAlarms());
		}
		return scrTblAlarms;
	}

	/**
	 * This method initializes tblAlarms	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getTblAlarms() {
		if (tblAlarms == null) {
			tblAlarms = new JTable(new AlarmTableModel(AlarmRegistry.getInstance().getActiveAlarms()));
			
		}
		return tblAlarms;
	}

	private class AlarmTableModel extends DefaultTableModel {
		private static final long serialVersionUID = 5663671440582146650L;
		public static final int REMOVE_SELECTED_COLUMN_INDEX = 5;
		private List<Alarm> alarms = null;
		private boolean[] removeSelected = null;
		
		public AlarmTableModel(List<Alarm> alarms) {
			if(alarms == null)
				alarms = new ArrayList<Alarm>();
			
			this.alarms = alarms;
			this.removeSelected = new boolean[alarms.size()];
			
			setColumnIdentifiers(new String[]{"Target ip", "Tipo campionamento", "Variabile",  "Soglia salita", "Soglia discesa", "Rimuovere"});
		}
		
		@Override
		public Object getValueAt(int row, int column) {
			if(alarms.size() <= 0 || row >= alarms.size())
				return null;

			Alarm alarm = alarms.get(row);
			
			switch (column) {
			case -1: return alarm;
			case 0: return alarm.getTargetIp();
			case 1: return alarm.getSampleType();
			case 2: return alarm.getVariable();
			case 3: return alarm.getRisingThreshold();
			case 4: return alarm.getFallingThreshold();
			case REMOVE_SELECTED_COLUMN_INDEX: return new Boolean(removeSelected[row]);
			}
			
			return super.getValueAt(row, column);
		}
		
		@Override
		public Class<?> getColumnClass(int columnIndex) {
			if(columnIndex == REMOVE_SELECTED_COLUMN_INDEX)
				return Boolean.class;
			
			return super.getColumnClass(columnIndex);
		}
		
		@Override
		public int getRowCount() {
			return (alarms == null) ? 0 : alarms.size();
		}
		
		@Override
		public boolean isCellEditable(int row, int column) {
			if(column == REMOVE_SELECTED_COLUMN_INDEX)
				return true;
			return false;
		}
		@Override
		public void setValueAt(Object value, int row, int column) {
			if(column == REMOVE_SELECTED_COLUMN_INDEX) {
				if(value instanceof String)
					removeSelected[row] = Boolean.valueOf((String) value);
				else
					removeSelected[row] = (Boolean) value;
			}
			super.setValueAt(value, row, column);
		}
	}
	/**
	 * This method initializes lblGeneralText	
	 * 	
	 * @return javax.swing.JLabel	
	 */
	private JTextArea getLblGeneralText() {
		if (lblGeneralText == null) {
			lblGeneralText = new JTextArea();
			lblGeneralText.setText("Di seguito viene riportato l'elenco completo degli alarm che si stanno attualmente monitorando\ne che non sono stati ancora sollevati:");
			lblGeneralText.setBounds(12, 10, 476, 49);
			lblGeneralText.setBorder(null);
			lblGeneralText.setOpaque(false);
			lblGeneralText.setEditable(false);
			lblGeneralText.setAutoscrolls(false);
			lblGeneralText.setFont(new JLabel().getFont());
		}
		return lblGeneralText;
	}
	
	private JButton getBttRemoveSelected() {
		if(bttRemoveSelected == null) {
			bttRemoveSelected = new JButton();
			bttRemoveSelected.setText("Rimuovi");
			bttRemoveSelected.setBounds(288, 235, 90, 27);
			bttRemoveSelected.setIcon(new ImageIcon("images/Remove.png"));
			bttRemoveSelected.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					AlarmTableModel model = (AlarmTableModel) getTblAlarms().getModel();
					int count = model.getColumnCount();
					List<Alarm> alarmsToRemove = new ArrayList<Alarm>();
					
					for (int i = 0; i < count; i++) {
						Boolean isRemove = (Boolean) model.getValueAt(i, AlarmTableModel.REMOVE_SELECTED_COLUMN_INDEX);
						if(isRemove != null && isRemove)
							alarmsToRemove.add((Alarm) model.getValueAt(i, -1));
					}
					
					if(alarmsToRemove.size() == 0)
						FrameWork.sendMessage("Nessun alarm indicato per la rimozione.", "Attenzione", JOptionPane.WARNING_MESSAGE);
					else {
						int confirm = JOptionPane.showConfirmDialog(RemoveAlarmPanel.this, "Sicuro di voler disattivare gli alarm indicati?", "Attenzione", JOptionPane.YES_NO_CANCEL_OPTION);
						switch (confirm) {
						case JOptionPane.OK_OPTION:
							if(confirm == JOptionPane.OK_OPTION) {
								AlarmRegistry registry = AlarmRegistry.getInstance();
								for (int j = 0; j < alarmsToRemove.size(); j++) {
									registry.unregisterAlarm(alarmsToRemove.get(j));
								}
							}
							close();
							break;
						case JOptionPane.NO_OPTION:
							close();
							break;
						case JOptionPane.CANCEL_OPTION: 
						case JOptionPane.CLOSED_OPTION: 
							default: break;
						}
					}
				}
			});
		}
		return bttRemoveSelected;
	}
	
//	private JButton getBttRemoveAll() {
//		if(bttRemoveAll == null) {
//			bttRemoveAll = new JButton();
//			bttRemoveAll.setText("Rimuovi tutto");
//			bttRemoveAll.setBounds(230, 247, 68, 22);
//		}
//		return bttRemoveAll;
//	}
	
	private JButton getBttClose() {
		if(bttClose == null) {
			bttClose = new JButton();
			bttClose.setText("Chiudi");
			bttClose.setBounds(392, 235, 90, 27);

			bttClose.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					close();
				}
			});
		}
		return bttClose;
	}
	
	public void close() {
		//Chiude il pannello
		Container container = this.getRootPane().getParent();
//		if(container != null && container instanceof JDialog) {
			((JDialog)container).dispose();
//		} else
//			container.setVisible(false);
	}
}
